#ifndef QTSPRITE_H
#define QTSPRITE_H

#include <QGraphicsItem>
#include "QtGroupAnimationEffect.h"

class QtSprite: public QObject, protected QGraphicsItem
{
	Q_OBJECT

public:
	QtSprite(QtSprite* parent = 0);
	virtual ~QtSprite();

	void AddToScene(QGraphicsScene *scene);

	void SetPosition(const QPointF& newPos);
	QPointF Position() const;

	void SetAlpha(qreal alpha);
	qreal Alpha() const;
	
	void SetAngle(qreal newAngle);
	qreal Angle() const;

	void Hide();
	void Show();

	void SetScale(unsigned int width, unsigned int height);
	unsigned int ScaleWidth() const;
	unsigned int ScaleHeight() const;
	unsigned int Width() const;
	unsigned int Height() const;

	void SetDepth(qreal z);
	qreal Depth();

	void AddSprite(QtSprite* sprite);

	void Center();
	void SetCenterPoint(const QPointF& point);
	const QPointF& CenterPoint() const;

	QPointF MapToScene(const QPointF& point) const;
	QPointF MapFromScene(const QPointF& point) const;

	void AddEffect(QtAnimationEffect *effect);
	void Start();

	void MoveTo(const QPointF& pos, unsigned int duration);
	void RotateRightTo(qreal angle, unsigned int duration);
	void RotateLeftTo(qreal angle, unsigned int duration);
	void ScaleTo(unsigned int width, unsigned int height, unsigned int duration);

protected:
	virtual void AlphaUpdated(qreal alpha);
	virtual void PositionUpdated(const QPointF& position);

	virtual QRectF boundingRect() const = 0;
	virtual QPainterPath shape() const = 0;
	virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) = 0;

private slots:
	void EmitFinished();
	void EmitStarted();

signals:
	void Finished();
	void Started();

private:
	void SetTransform();
	

	qreal alpha;
	qreal angle;
	QtGroupAnimationEffect effects;
	QPointF scale;
	QPointF centerPoint;

	QTransform scaleMatrix, rotationMatrix, translationMatrix;
	std::list<QtSprite*> children;
};

#endif //QTSPRITE_H
